package adverts_api

import (
	"gin_study_blog/common"
	"gin_study_blog/common/http"
	"gin_study_blog/models"
	"github.com/gin-gonic/gin"
)

func (AdvertsApi) AdvertsModifyView(c *gin.Context) {
	id := c.Param("id")
	var cr AdvertRequest
	// 二选一
	//err := c.ShouldBindBodyWith(&cr, binding.JSON)
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		http.FailWithError(err, &cr, c)
		return
	}
	var adverts models.AdvertModel
	err = common.DB.Take(&adverts, id).Error
	if err != nil {
		http.FailWithMessage("广告不存在", c)
		return
	}

	// 这儿修改需要使用到 Updates 和 map 来修改, 如果使用正常的Update就 is_show = false 就不会修改
	err = common.DB.Model(&adverts).Where("id = ?", id).Updates(map[string]any{
		"title":   cr.Title,
		"href":    cr.Href,
		"images":  cr.Images,
		"is_show": cr.IsShow,
	}).Error
	if err != nil {
		common.Log.Error(err)
		http.FailWithMessage("修改失败", c)
		return
	}
	http.IsOkWithMessage("修改广告成功", c)
}
